#coding=utf-8
__author__ = 'chutf'
__date__ = '2020/3/22 13:54'

from  random import Random
from  django.core.mail import  send_mail

from  users.models import EmailVerifyRecord
from  MxOnline.settings import EMAIL_FROM



def random_str(randomlength=8):
    str=''
    chars='AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuWwXxYyZz0123456789'
    length=len(chars)-1
    random=Random()
    for i in range(randomlength):
        str+=chars[random.randint(0,length)]
    return str

# if __name__== "__main__":
#     print(random_str(10));


def send_register_email(email,send_type="register"):
    email_record=EmailVerifyRecord()
    if send_type=="update_email":
        code=random_str(4)
    else:
        code=random_str(16)
    email_record.code=code
    email_record.email=email
    email_record.send_type=send_type
    email_record.save()

    email_title=""
    email_body=""

    if send_type=="register":
        email_title="测试学院注册激活链接-自动发送的(请勿回复)"
        email_body="请点击下面的链接进行激活你的账号：http://127.0.0.1:8888/active/{0}".format(code)
        send_status=send_mail(email_title,email_body,EMAIL_FROM,[email])
        if send_status:
            pass
    elif send_type=="forget":
        email_title="测试学院的密码重置链接-自动发送的(请勿回复)"
        email_body="请点击下面的链接进行重置你的账户密码：http://127.0.0.1:8888/reset/{0}".format(code)
        send_status=send_mail(email_title,email_body,EMAIL_FROM,[email])
        if send_status:
            pass
    elif send_type=="update_email":
        email_title="测试学院的修改邮箱用的验证码-自动发送的(请勿回复)"
        email_body="你的邮箱验证码为：{0}".format(code)
        send_status=send_mail(email_title,email_body,EMAIL_FROM,[email])
        if send_status:
            pass

















